<template>
  <div class="MyEntryOrders">
    <div class="form-content">
      <x-input :placeholder="$t('tradenum')" class="num" v-model="cny" keyboard="number"></x-input>
      <x-input :placeholder="$t('tadepwd')" class="num" type="password"  v-model="pay_password"></x-input>
      <div class="list-check">
        <div class="female">
          <input type="radio" id="female" name="type" v-model="type" value="0">
          <label for="female">{{$t('my purchase')}}</label>
        </div>
        <div class="male">
          <input type="radio" id="male" name="type" v-model="type" value="1">
          <label for="male">{{$t('my sale')}}</label>
        </div>
      </div>
      <div>
        <x-button class="btn" @click.native="change">{{$t('confirm')}}</x-button>
      </div>
    </div>
    <div class="order-content">
      <tab>
        <tab-item :selected="parseInt(type) === 0" @on-item-click="type = 0">{{$t('purchaserecord')}}</tab-item>
        <tab-item :selected="parseInt(type) === 1" @on-item-click="type = 1">{{$t('sell record')}}</tab-item>
      </tab>
      <div class="drop-down">
        <select name="" v-model="status" @change="find">
          <option value="4" >{{$t('total')}}</option>
          <option value="2" >{{$t('trading')}}</option>
          <option value="1" >{{$t('untrading')}}</option>
          <option value="3" >{{$t('trade.finish')}}</option>
          <option value="0" >{{$t('paystatus.Cancel')}}</option>
        </select>
      </div>
      <!--<div class="wire"></div>-->
      <div class="wrapper">
        <scroller
          :on-refresh="refresh"
          :on-infinite="infinite"
          :noDataText="$t('no more')"
          ref="myScroller"
        >
          <div class="content">
            <div class="list" v-for="i in data">
              <ul>
                <li @click="goNext(i.deal_sn,i.status, type)">
                  <div class="left">
                    <p class="p2">
                      {{type === 0 ? $t('want by') : $t('want sell')}}
                      <!--:{{i.user_id}}-->
                    </p>
                    <p class="p4">{{$t('num')}}：{{i.cny}}</p>
                  </div>
                  <div class="right">
                    <p class="p4" :style="{color: i.color}">{{i.newStatus}}</p>
                    <p class="p3">{{i.create_time}}</p>
                  </div>
                </li>
                <p style="padding: 0 .4rem;color: #cc0000;line-height: 2" v-if="i.remark">{{$t('remarkmmessage')}}：{{i.remark}}</p>
                <p v-if="i.status === '2' && i.residue_cny > 0" style="padding: 0 .4rem;color: #cc0000;line-height: 2">{{$t('unmatch')}}：{{i.residue_cny}}</p>
                <div v-if="i.status === '1'">
                  <x-button style="" class="btn" @click.native="cancleTrade(i.deal_sn)">{{$t('cancel btn')}}</x-button>
                </div>
              </ul>
            </div>
            <no-data :nomore="false" v-if="showNodata"></no-data>
          </div>
          <!--<div class="content maichu" v-if="showTab === 2">-->
          <!--<div class="list" v-for="i in data">-->
          <!--<ul>-->
          <!--<li @click="goNext(i.deal_sn,i.status, 2)">-->
          <!--<div class="left">-->
          <!--<p class="p2">{{$t('buyer')}}:{{i.user_id}}</p>-->
          <!--<p class="p4">{{$t('num')}}：{{i.cny}}</p>-->
          <!--</div>-->
          <!--<div class="right">-->
          <!--<p class="p4" :style="{color: i.color}">{{i.status}}</p>-->
          <!--<p class="p3">{{i.create_time}}</p>-->
          <!--</div>-->
          <!--</li>-->
          <!--<div class="btn" v-if="i.status === '未交易'"><button @click="cancleTrade(i.deal_sn)">{{$t('cancel btn')}}</button></div>-->
          <!--</ul>-->
          <!--</div>-->
          <!--<no-data v-if="data.length === 0"></no-data>-->
          <!--</div>-->
        </scroller>
      </div>
    </div>
  </div>
</template>

<i18n>
    title.myorder:
      en: Trade Market
      zh-CN: 交易市场
    buy:
      en: Purchase Order
      zh-CN: 买单挂单
    sell:
      en: Sale Order
      zh-CN: 卖单挂单
    tradenum:
      en: Trade Num
      zh-CN: 请输入交易数量
    tadepwd:
      en: Trade Passward
      zh-CN: 请输入交易密码
    listing success:
      en: Listing success
      zh-CN: 挂单成功
    make sure:
      en: Click to make sure that you can continue the list
      zh-CN: 点击确定您可以继续挂单
    paystatus.trading:
      en: Trading
      zh-CN: 交易中
    paystatus.untraded:
      en: Untraded
      zh-CN: 未交易
    paystatus.Cancel:
      en: Cancel
      zh-CN: 取消交易
    paystatus.already:
      en: Pay Already
      zh-CN: 已付款
    unmatch:
      en: Unmatch
      zh-CN: 未匹配数量
    purchaserecord:
      en: Purchase Record
      zh-CN: 购买记录
    sell record:
      en: Sell Record
      zh-CN: 出售记录
    trade.finish:
      en: Finished
      zh-CN: 已完成
    no.neednum:
      en: Please input num
      zh-CN: 请输入交易数量
    no.tradepassword:
      en: Please input tradepassword
      zh-CN: 请输入交易数量
    tradetips:
      en: Message
      zh-CN: 留言
    inputmessage:
      en: Input message
      zh-CN: 请输入留言
    remarkmmessage:
      en: Remark
      zh-CN: 留言
</i18n>

<script>
  import {XInput, Group, XButton, CheckIcon, Tab, TabItem} from 'vux'
  import NoData from '../components/nodata'
  export default {
    name: 'MyEntryOrders',
    components: {
      XInput,
      Group,
      XButton,
      CheckIcon,
      Tab,
      TabItem,
      NoData
    },
    data () {
      return {
        cny: '',
        type: 0,
        pay_password: '',
        scrollLoading: false,
        page: 1,
        listRows: 5,
        status: 4,
        data: [],
        showNodata: false,
        language: localStorage.language

      }
    },
    methods: {
      change () {
        let vue = this
        if (vue.cny === '') {
          vue.$vux.toast.text(vue.$t('no.neednum'))
          return false
        }
        if (vue.pay_password === '') {
          vue.$vux.toast.text(vue.$t('no.tradepassword'))
          return false
        }
        if (parseInt(vue.type) === 1) {
          this.$vux.confirm.show({
            title: vue.$t('tradetips'),
            showInput: true,
            placeholder: vue.$t('inputmessage'),
            onCancel () {
            },
            onConfirm (msg) {
              vue.payRequest(msg)
            }
          })
        } else {
          vue.payRequest()
        }
      },
      payRequest (msg) {
        let vue = this
        this.post({
          url: '/api/deal/makedeal',
          params: {
            token: localStorage.token,
            cny: vue.cny,
            type: vue.type,
            pay_password: vue.pay_password,
            remark: msg
          },
          success: function (data) {
            if (data.code === 0) {
              vue.$vux.toast.text(data.msg)
            } else {
              vue.data = []
              vue.cny = ''
              vue.pay_password = ''
              vue.$vux.toast.text(vue.$t('listing success'))
              vue.getInfo()
            }
          }
        })
      },
      refresh (done) {
        this.page = 1
        this.data = []
        this.getInfo(done)
      },
      infinite (done) {
        if (!this.scrollLoading) {
          this.getInfo(done)
        } else {
          this.$refs.myScroller.finishInfinite(true)
        }
      },
      goNext (id, status, type) {
        if (status === '3') {
          this.$router.push({path: '/tradepaycoin', query: {num: id, type: type}})
        }
        if (status === '2') {
          this.$router.push({path: '/tradepaycoin', query: {num: id, type: type}})
        }
      },
      find () {
        this.page = 1
        this.data = []
        this.getInfo()
      },
      cancleTrade (num) {
        let vue = this
        this.$vux.confirm.show({
          title: this.$t('info'),
          content: this.$t('content'),
          onConfirm () {
            vue.post({
              url: `/api/usercenter/cancelDeal`,
              params: {
                token: localStorage.token,
                deal_sn: num
              },
              success: function (data) {
                if (data.code === 1) {
                  vue.$vux.toast.show({
                    text: `撤销成功`
                  })
                  vue.data.forEach(function (val, index) {
                    console.log(val)
                    if (val.deal_sn === num) {
                      val.status = '取消交易'
                      val.color = ''
                    }
                    return val
                  })
                  vue.data = []
                  vue.getInfo()
                } else if (data.code === 0) {
                  vue.$vux.toast.show({
                    text: `data.data.msg`
                  })
                }
              }
            })
          }
        })
      },
      getInfo (done) {
        let vm = this
        this.scrollLoading = true
        let showLoding
        vm.page === 1 ? showLoding = true : showLoding = false
        this.post({
          url: `/api/logging/mydeallog`,
          showloading: showLoding,
          params: {
            token: localStorage.token,
            type: vm.type,
            listRows: vm.listRows,
            page: vm.page,
            status: vm.status
          },
          success: (data) => {
            console.log(data)
            if (data.code === 0) {
              this.$vux.toast.show({
                text: `${data.msg}`,
                type: 'text'
              })
              vm.$refs.myScroller.finishInfinite(true)
              done()
            } else if (data.code === 1) {
              let newData = data.data.data
              data.data.data.length === 0 ? vm.showNodata = true : vm.showNodata = false
              console.log(newData)
              if (newData.length < vm.listRows) {
                vm.scrollLoading = true
                // vm.data = vm.data.concat(data.data.data)
                vm.$refs.myScroller.finishInfinite(true)
              } else {
                vm.scrollLoading = true
                vm.page = vm.page + 1
              }
              let midData = newData.map(function (val, index) {
                if (val.status === '2') {
                  val.color = '#57a94f'
                  val.newStatus = vm.$t('paystatus.trading')
                } else if (val.status === '1') {
                  val.color = '#fbb752'
                  val.newStatus = vm.$t('paystatus.untraded')
                } else if (val.status === '3') {
                  val.newStatus = vm.$t('trade.finish')
                } else {
                  val.newStatus = vm.$t('paystatus.Cancel')
                }
                val.end = true
                return val
              })
              for (let i = 0; i < midData.length; i++) {
                this.data.push(midData[i])
              }
              done()
            }
          }
        })
      }
    },
    watch: {
      type: function (val, oldVal) {
        this.data = []
        this.page = 1
        this.getInfo()
      }
    }
  }
</script>

<style scoped>

  .list-check{
    display: flex;
  }
  .female{
    flex-grow: 1;
    padding: .2rem .4rem;
  }
  .male{
    flex-grow: 1;
    padding: .2rem;
  }
  label{
    font-size: 0.28rem;
  }
  input[type="radio"] + label::before {
    content: "\a0"; /*不换行空格*/
    display: inline-block;
    vertical-align: middle;
    font-size: 18px;
    width: 0.5em;
    height: 0.5em;
    margin-right: .4em;
    border-radius: 50%;
    border: 1px solid #537fbf;
    text-indent: .15em;
    line-height: 1;
    padding: .2em;
  }
  input[type="radio"]:checked + label::before {
    background-color: #537fbf;
    background-clip: content-box;
    padding: .2em;
  }
  input[type="radio"] {
    position: absolute;
    clip: rect(0, 0, 0, 0);
  }
  .weui-cell:before{
    border-top: none!important;
  }
  .num{
    width: 6.7rem;
    border-bottom: 1px solid #fafafa!important;
    margin: 0 auto;
    color: #bbbbbb;
    font-size: .28rem;
  }
  .btn{
    width: 6.7rem;
    background-color: #537fbf;
    color: #ffffff;
    font-size: .28rem;
    /*margin-top: 1rem;*/
    margin: 0 auto;
  }
  .form-content {
    padding-bottom: .2rem;
    background-color: #fff;
  }
  .drop-down{
    height: .8rem;
    background-color: #fafafa;
  }
  .drop-down select{
    appearance: none;
    outline: none;
    width: 1.85rem;
    height: .8rem;
    display: inline-block;
    color: #666666;
    background-color:#fafafa;
    margin-right: 0.5rem;
    padding-left: 0.3rem;
    border: none;
    font-size: 0.28rem;
    float: right;
    background: url("../assets/img/down.png") no-repeat scroll right center transparent;
  }
  .content .list{
    /*border-bottom: 1px solid red;*/
  }
  .content .list ul{
    border-bottom: 0.01rem solid #eeeeee;
    padding: .1rem 0;
  }
  .content .list .ul3{
    border-bottom:none;
  }
  .content .list li{
    display: flex;
  }
  .content .list .left{
    flex-grow: 1;
    padding-left: 0.4rem;
  }
  .content .list .right{
    flex-grow: 1;
    width: 2.5rem;
    text-align: right;
    padding-right: 0.4rem;
  }
  .content p{
    line-height: 2;
  }
  .content .list .p3{
    font-size: 0.2rem;
  }
  .content .list .p3,.p4{
    color: #999;
  }
  .content .list .p5{
    color: #fbb752;
  }
  .content .list .p6{
    color: #57a94f;
  }
  .content .btn{
    width: 6.7rem;
    margin: 0 auto;
  }
  .order-content {
    height: 63vh;
    border-top: 1px solid #dfdfdf;
  }
  .order-content .wrapper {
    height: calc(100% - 44px - .8rem);
    position: relative;
  }
</style>
